<?php
/**
 * Created by PhpStorm.
 * User: l-ht
 * Date: 2018/11/12
 * Time: 11:33
 */

namespace app\admin\controller;

error_reporting(0);

use app\index\model\Post;
use app\index\model\Thread;
use think\Request;
use think\Db;

class Index extends Base
{

    //主页
    public function index()
    {
        return view('index');
    }

    //爆笑段子
    public function sourse(Request $request)
    {
        $data = $request->param();

        $sourse = Db::name('digital')
            ->where(function ($query) use ($data) {
                $data['type'] > 0 && $query->where('type',$data['type']);
                $data['title'] && $query->where('title','like',"%{$data['title']}%");
            })
            ->order("id","desc")
            ->paginate(20,false,[
                'query' => $request->param()
            ]);
        return view('sourse',compact('sourse'));
    }


    //删除资源
    public function delSour()
    {
        $id = \request()->post('id');
        $res = Db::name('digital')->where('id',$id)->delete();

        if ($res) {
            return $this->_toSuccess();
        }else{
            return $this->_toError();
        }

    }
    
    
    //积分列表明细
    public function points()
    {
        $points = Db::name("point")
            ->alias("p")
            ->join("user u","p.uid = u.id")
            ->order("p.id","desc")
            ->field("u.nickname,u.avatar,u.point,u.id as uid,p.id,p.digital_id,p.number,p.remark,p.type,p.created_at")
            ->paginate(20,false,[
                'query' => \request()->param()
            ]);
//        dump($points);

        return view('points',compact('points'));
    }

    //操作积分提现
    public function desPoint()
    {

        $phone = \request()->post("phone");
        $number = \request()->post("number");

        $check = Db::name("user")
            ->where("phone",$phone)
            ->find();
        //echo $number.'-'.$check['point'];
        //echo $check['point'] - $number ;die;
        if ($check['point'] - $number <= 0 ) {
            echo "<script>alert('金币不足!');window.location.href='/admin/index/points'</script>";
            die;
        }

        Db::name("user")->where("id",$uid)->setDec('point',$number);

        $sta = Db::name("point")
            ->insert([
                'uid'    => $uid,
                'type'   => 2,
                'number' => $number,
                'remark' => '金币提现',
                'created_at' => date("Y-m-d H:i:s",time())
            ]);

        if ($sta) {

            echo "<script>alert('提现成功!');window.location.href='/admin/index/points'</script>";

        } else {
            Db::name("user")->where("uid",$uid)->setInc('point',$number);
            echo "<script>alert('提现失败!');window.location.href='/admin/index/points'</script>";

        }
    }

    //管理员管理
    public function adminlists()
    {
        $list = Db::name("admin")->select();
        return view("adminlists",compact('list'));
    }



    ######################################
    //全部帖子
    public function thread(Request $request)
    {
        $data = $request->param();

        $threads = Thread::where(function ($query) use ($data) {
            //if($data['begintime']) echo strtotime($data['begintime']);die;
            $data['typeid'] > 0 && $query->where('typeid',$data['typeid']);
            $data['begintime']  && $query->where('create_time','>=',strtotime($data['begintime']));
            $data['endtime']    && $query->where('create_time','<=',strtotime($data['endtime']) + 3600*24);
            $data['keywords']   && $query->where('subject','like',"%{$data['keywords']}%");

        })->order('tid','desc')->field(['tid','authorid','typeid','subject','replies','region','district','create_time','digest','closed'])->paginate(20,false,[
            'query' => $request->param()
        ]);

        //dump($threads);die;
        return view('thread',compact('threads'));
    }
    
    //删除帖子
    public function delThread()
    {
        $id = \request()->post('id');
        $res = Thread::where('tid',$id)->delete();

        if ($res) {
            return $this->_toSuccess();
        }else{
            return $this->_toError();
        }

    }

    //加精
    public function godThread()
    {
        $id = \request()->post('id');
        $res = Thread::where('tid',$id)->update(['digest' => 1]);

        if ($res) {
            return $this->_toSuccess();
        }else{
            return $this->_toError();
        }
    }

    //显示or隐藏
    public function displayThread()
    {
        $id = \request()->post('id');
        $data = \request()->post('data');
        $res = Thread::where('tid',$id)->update(['closed' => $data]);

        if ($res) {
            return $this->_toSuccess();
        }else{
            return $this->_toError();
        }
    }

    //查看回复
    public function reply()
    {
        $tid = \request()->get('tid');

        $posts = Post::where('tid',$tid)->select();

        return view('reply',compact('posts'));
    }

    //删除回复
    public function delReply()
    {
        $pid = \request()->post('id');

        $post = Post::where('pid',$pid);
        $tid = $post->find()['tid'];
        $res = $post->delete();

        if ($res) {
            Thread::where('tid',$tid)->setDec('replies',1);
            return $this->_toSuccess();
        }else{
            return $this->_toError();
        }
    }
}